﻿using System;
using Aocsoft.Infrastructure;
using Aocsoft.Infrastructure.Repository.NHContext;
using AocsoftSample.PersistModels;
using NHibernate;
using NHibernate.Cfg;
using NUnit.Framework;

namespace RepositoriesTest
{
    [TestFixture]
    public class CategoryRepositoryTest : RepositoryTestBase
    {
        [SetUp]
        public void SetUp()
        {
            Console.WriteLine("SetUp");

            MSessionFactory.RegisterSessionType(System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, @"hibernate.cfg.xml"));
            CreateInitialData();
            Console.WriteLine("\r\nStart Testing...");
        }

        [TearDown]
        public void TearDown()
        {
            Console.WriteLine("Finished Testing...\r\n");
            Console.WriteLine("TearDown");

            var session = MSessionFactory.GetNewSessionInstance();

            session.Delete("from Category");
            session.Flush();
        }

        Category _root, _child1, _child2, _child1_1, _child1_2, _child1_3, _child2_1, _child2_2, _child2_1_1, _child2_1_2;
        private void CreateInitialData()
        {
            _root = new Category { Name = "root" };
            _child1 = new Category { Name = "child1" };
            _child2 = new Category { Name = "child2" };
            _child1_1 = new Category { Name = "grand child 1-1" };
            _child1_2 = new Category { Name = "grand child 1-2" };
            _child1_3 = new Category { Name = "grand child 1-3" };
            _child2_1 = new Category { Name = "grand child 2-1" };
            _child2_2 = new Category { Name = "grand child 2-2" };

            _child2_1_1 = new Category { Name = "grand child 2-1-1" };

            _child2_1_2 = new Category { Name = "grand child 2-1-2" };

            _root.AddChild(_child1);
            _root.AddChild(_child2);
            _child1.AddChild(_child1_1);
            _child1.AddChild(_child1_2);
            _child1.AddChild(_child1_3);
            _child2.AddChild(_child2_1);
            _child2.AddChild(_child2_2);

            _child2_1.AddChild(_child2_1_1);
            _child2_1.AddChild(_child2_1_2);

            var session = MSessionFactory.GetNewSessionInstance();
            session.Save(_root);
            session.Flush();
            session.Dispose();
        }

        [Test]
        public void AddChild_and_RemoveChild_Test()
        {
            var id = _child2_1_2.Key;
            using (var session = MSessionFactory.GetNewSessionInstance())
            {
                session.Delete(session.Load<Category>(id));
                session.Flush();
            }
        }
    }

    public abstract class RepositoryTestBase
    {
        [TestFixtureSetUp]
        public void TestInit()
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-cn", true);
            Console.WriteLine("Current UI Culture:{0}", System.Threading.Thread.CurrentThread.CurrentUICulture);
            Console.WriteLine("TestFixtureSetUp");
        }

        [TestFixtureTearDown]
        public void TestTearDown()
        {
            Console.WriteLine("TestFixtureTearDown");
        }
    }
}
